package com.baichang.taoshunda.service.impl;


import com.baichang.framework.Code;
import com.baichang.framework.excelption.DBException;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.framework.utils.LogUtils;
import com.baichang.taoshunda.dao.CityDao;
import com.baichang.taoshunda.entity.City;
import com.baichang.taoshunda.entity.CityTag;
import com.baichang.taoshunda.service.CityService;
import com.baichang.taoshunda.utils.ParserTool;
import com.baichang.taoshunda.utils.PinYinUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CityServiceImpl implements CityService {

    @Autowired
    private CityDao cityDao;


    @Override
    public Map<String, Object> getCity(int parentId, String key) {
        Map<String, Object> map = new HashedMap();
        //获取所有的热门城市
        List<Map<String, Object>> hotCity = cityDao.getHotCity();
        //获取城市
        List<Map<String, Object>> cityList = cityDao.getCityList(parentId, key);
        map.put("hotCity", hotCity);
        map.put("cityList", cityList);
        return map;
    }

    /**
     * 根据省会编号获取下级城市
     *
     * @param parentId 省会编号 （预留，现在用不到）
     * @return
     */
    @Override
    public List<Map<String, Object>> getCityByParentId(int parentId) {
        String key = "";
        List<Map<String, Object>> cityList = cityDao.getCityList(parentId, key);
        return cityList;
    }

    /**
     * 根据上级编号获取城市信息
     *
     * @param parentId 上级编号
     * @return
     */
    @Override
    public List<Map<String, Object>> getDeliveryCityByParentId(int parentId) {
        return cityDao.getDeliveryCityByParentId(parentId);
    }

    /**
     * 获取所有的热门城市
     */
    @Override
    public Page<Map<String, Object>> queryHotCity(int nowPage, int pageSize) throws DBException {
        return cityDao.queryHotCity(nowPage, pageSize);
    }


    /**
     * 修改排序
     */
    public boolean updateSorting(int id, int sorting) throws ServiceException {
        if (id == 0) {
            throw new ServiceException("编号错误");
        }
        if (cityDao.update(sorting, id) > 0) {
            return true;
        }
        return false;
    }


    /**
     * 删除城市
     *
     * @param id 编号
     * @return
     */
    @Override
    public boolean delete(int id) throws ServiceException {
        if (id <= 0) {
            throw new ServiceException("编号错误");
        }
        int num = cityDao.findCategoryBySuperId(id);
        if (num != 0) {
            throw new ServiceException("拥有子集，请删除子集");
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", 0);
        if (cityDao.update(map, id) > 0) {
            LogUtils.getInstance().info("删除城市,编号为：" + id);
            return true;
        }
        return false;
    }

    /**
     * 修改城市的名称
     */
    @Override
    public boolean updateCityName(int id, String cityName) throws ServiceException {

        //首先查询一下，还有没有其它的同名城市
        int num = cityDao.querySameCity(id, cityName);
        if (num != 0) {
            throw new ServiceException("已经有其它的同名的城市");
        } else {

            cityDao.updateCityName(id, cityName);
            return true;
        }
    }

    /**
     * 后台查询城市
     */
    @Override
    public Page<City> queryAllCity(int nowPage, int pageSize, int parentId, int isHot, String name) throws DBException {
        return cityDao.queryAllCity(nowPage, pageSize, parentId, isHot, name);
    }


    /**
     * 整合lupei
     * 添加和修改
     */
    @Override
    public boolean addCity(int id, String name, Long parentId, Integer deptPath, Integer sorting) throws ServiceException {

        if (name == null) {
            throw new ServiceException(Code.code_params_error, "城市名称不能为空");
        }

        //不管是修改还是添加，该parentId下面不能有重名的地区

        List<City> list = cityDao.querySameName(name, parentId, id);


//        List<City> list = cityDao.queryName(name);

        if (list != null && list.size() > 0) {
            throw new ServiceException("您添加的地区重名");
        }

        if (parentId == 0) {
            if (id == 0) {
                City city = new City();
                city.setName(name);
                city.setCreated(new Date());
                city.setParentId(parentId);
                city.setStatus(1);
                city.setDeptPath(1);
                city.setIsHot(CityTag.否.getTagVersion());
                city.setIsOpen(2);
                city.setSorting(sorting);
                //增加城市拼音
                city.setInitials(PinYinUtil.getFirstSpell(name));
                if (cityDao.save(city) > 0) {
                    LogUtils.getInstance().info("添加城市：" + name);
                    return true;
                }

            } else {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("name", name);
                map.put("initials", PinYinUtil.getFirstSpell(name));
                if (cityDao.update(map, id) > 0) {
                    LogUtils.getInstance().info("修改城市信息，编号为：" + id);
                    return true;
                }
            }
        } else {
            if (id == 0) {
                City city = new City();
                city.setName(name);
                city.setCreated(new Date());
                city.setParentId(parentId);
                city.setStatus(1);
                city.setDeptPath(deptPath);
                city.setIsHot(CityTag.否.getTagVersion());
                city.setIsOpen(2);
                city.setSorting(sorting);
                city.setInitials(PinYinUtil.getFirstSpell(name));
                if (cityDao.save(city) > 0) {
                    LogUtils.getInstance().info("添加城市：" + name);
                    return true;
                }
            } else {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("name", name);
                map.put("initials", PinYinUtil.getFirstSpell(name));
                if (cityDao.update(map, id) > 0) {
                    LogUtils.getInstance().info("修改城市信息，编号为：" + id);
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 设置热门
     */
    @Override
    public boolean addHot(int id, int isHot) throws ServiceException {
        if (id == 0) {
            throw new ServiceException("编号错误");
        }

        if (isHot == 2) {
            //查看热门的城市现在已经有几个了
            int count = cityDao.hotCityCount();
            if (count >= 6) {
                throw new ServiceException("热门城市的个数已到达上限六个");
            }
        }

        if (isHot != 2) {
            isHot = 2;
        } else {
            isHot = 1;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("isHot", isHot);
        if (cityDao.update(map, id) > 0) {
            return true;
        }

        return false;
    }

    /**
     * 获取城市
     *
     * @param deptPath
     * @return
     */
    @Override
    public List<Map<String, Object>> queryByCityId(Integer cityId, Integer deptPath) {
        return cityDao.queryByCityId(cityId, deptPath);
    }

    /**
     * 根据省id获取市
     *
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> queryCityIdInfo(Integer id) {
        return cityDao.queryCityIdInfo(id);
    }

    @Override
    public String getCityNameById(Integer cityId) {
        City city = cityDao.getCityById(cityId);
        if (city != null) {
            return city.getName();
        }
        return "";
    }

    @Override
    public int getCityIdByCityName(String cityName) {

        return cityDao.getCityIdByCityName(cityName);
    }

    @Override
    public Map<String, Object> getCityByLngLat(Integer level, String lng, String lat) {
        String areaName = ParserTool.getAdd(lng, lat);
        Map<String, Object> map = new HashedMap();
        map.put("id", cityDao.getCityIdByName(areaName, level));
        map.put("cityId", cityDao.getCityIdByName(areaName, level));
        return map;
    }
}